﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ggdll.GGMessages.Outgoing
{
    class GGMessageOutgoingMessage
    {
        private static int message_type = (int)GGMessageType.GG_SEND_MSG80;
        public int recipient;
        private int seq=(int)(DateTime.UtcNow - new DateTime(1970,1,1,0,0,0)).TotalSeconds;
        private int messageclass=0x08;
        private int offset_plain;
        private int offset_attributes;
        private string html;
        public string plain_message;
        private byte[] attributes={0x02,0x06, 0x00, 0x00, 0x00, 0x08, 0x00,0x00,0x00};

        public byte[] getBytes()
        {
            int message_size = 4 + 4 + 4 + 4 + 4 + 82 + 2 + 2 * plain_message.Length + 9;
            offset_plain = 103 + plain_message.Length;
            offset_attributes = offset_plain + plain_message.Length + 1;
            byte[] answer = new byte[message_size + 8];
            System.Buffer.BlockCopy(BitConverter.GetBytes(message_type), 0, answer, 0, 4);
            System.Buffer.BlockCopy(BitConverter.GetBytes(message_size), 0, answer, 4, 4);
            System.Buffer.BlockCopy(BitConverter.GetBytes(recipient), 0, answer, 8, 4);
            System.Buffer.BlockCopy(BitConverter.GetBytes(seq), 0, answer, 12, 4);
            System.Buffer.BlockCopy(BitConverter.GetBytes(messageclass), 0, answer, 16, 4);
            System.Buffer.BlockCopy(BitConverter.GetBytes(offset_plain), 0, answer, 20, 4);
            System.Buffer.BlockCopy(BitConverter.GetBytes(offset_attributes), 0, answer, 24, 4);
            html=@"<span style=""color:#000000; font-family:'MS Shell Dlg 2'; font-size:9pt; "">"+plain_message+"</span>"+'\0';
            StaticVars.enc.GetBytes(html, 0, html.Length, answer, 28);
            plain_message += '\0';
            StaticVars.ASCIIenc.GetBytes(plain_message, 0, plain_message.Length, answer, offset_plain+8);
            System.Buffer.BlockCopy(attributes, 0, answer, offset_attributes+8, attributes.Length);
            return answer;
        }
    }
}